package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.VehicleTrailerCondition;
import com.kcwl.carrier.domain.entity.VehicleTrailerInfo;
import com.kcwl.carrier.domain.entity.VehicleTrailerInfoVo;
import com.kcwl.carrier.domain.repository.IVehicleTrailerRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.VehicleTrailerMapper;
import com.kcwl.carrier.infrastructure.repository.po.VehicleTrailerPO;
import com.kcwl.carrier.interfaces.query.PlateNumberQuery;
import com.kcwl.carrier.interfaces.query.VehicleTrailerQuery;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class VehicleTrailerRepositoryImpl extends BaseRepositoryImpl<VehicleTrailerMapper, VehicleTrailerPO> implements IVehicleTrailerRepository {

    @Resource
    private VehicleTrailerMapper vehicleTrailerMapper;


    @Override
    public Class<VehicleTrailerPO> getPoClass() {
        return VehicleTrailerPO.class;
    }

    @Override
    public void updateById(VehicleTrailerInfo vehicleTrailer) {
        vehicleTrailerMapper.updateById(KcBeanConverter.toBean(vehicleTrailer, VehicleTrailerPO.class));
    }

    @Override
    public List<VehicleTrailerInfo> getList(VehicleTrailerCondition condition) {
        QueryWrapper<VehicleTrailerPO> wrapper = new QueryWrapper<>();
        wrapper.eq("plate_number", condition.getPlateNumber());
        wrapper.eq("carrier_id", condition.getCarrierId());
        wrapper.eq("is_del", 0);
        List<VehicleTrailerPO> vehicleTrailerPOS = vehicleTrailerMapper.selectList(wrapper);
        return KcBeanConverter.toList(vehicleTrailerPOS, VehicleTrailerInfo.class);
    }

    @Override
    public List<VehicleTrailerInfo> getByIds(List<Long> ids) {
        QueryWrapper<VehicleTrailerPO> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        return KcBeanConverter.toList(vehicleTrailerMapper.selectList(wrapper), VehicleTrailerInfo.class);
    }

    @Override
    public KcPage<VehicleTrailerInfo> getTrailerList(PlateNumberQuery query) {
        IPage<VehicleTrailerInfo> iPage = vehicleTrailerMapper.getTrailerList(new Page<>(query.getCurPagerNo(), query.getPageSize()), query);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public KcPage<VehicleTrailerInfoVo> queryVehicleTrailerPage(VehicleTrailerQuery query) {
        IPage<VehicleTrailerInfoVo> iPage = vehicleTrailerMapper.queryVehicleTrailerPage(new Page<>(query.getCurPagerNo(), query.getPageSize()), query);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public VehicleTrailerInfo getTrailerDetail(Long id) {
        return KcBeanConverter.toBean(vehicleTrailerMapper.selectById(id), VehicleTrailerInfo.class);
    }
}
